package com.deng.permission.view
{
	import com.deng.common.vo.AdminDepartmentVo;
	import com.deng.common.vo.DispenseOrganizationVo;
	import com.deng.common.vo.OperateDepartmentVo;
	import com.deng.common.vo.OrganizationVo;
	import com.deng.event.ReturnEvent;
	import com.deng.permission.model.PermissionModelLocator;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.events.FlexEvent;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class RuleOrganizationCB extends CheckBox
	{
		private var pvo:Object;
		
		public function RuleOrganizationCB()
		{
			this.addEventListener(FlexEvent.CREATION_COMPLETE,showHandler);
			this.addEventListener(Event.CHANGE,showHandler);
			this.addEventListener(FlexEvent.SHOW,showHandler);//本来是2
			this.addEventListener(MouseEvent.CLICK,upEvt);
			super();
		}
		
		private function insertRst(evt:ResultEvent):void{
			if(evt.result as int == 1){
				Alert.show(ResourceManager.getInstance().getString("lang", "update_success"));
				if(PermissionModelLocator.getInstance().rule_name == "pf_gongzuojixiao"
					&& PermissionModelLocator.getInstance().tmpUserVo.pf_gongzuojixiao != null){
					PermissionModelLocator.getInstance().tmpUserVo.pf_gongzuojixiao+=(":"+pvo.id+":");
				}else if(PermissionModelLocator.getInstance().rule_name == "pf_kpi"
					&& PermissionModelLocator.getInstance().tmpUserVo.pf_kpi != null){
					
				}else if(PermissionModelLocator.getInstance().rule_name == "sh_gongzuojixiao"
					&& PermissionModelLocator.getInstance().tmpUserVo.sh_gongzuojixiao != null){
					PermissionModelLocator.getInstance().tmpUserVo.sh_gongzuojixiao+=(":"+pvo.id+":");
				}
//				else if(PermissionModelLocator.getInstance().rule_name == "sh_kpi"
//					&& PermissionModelLocator.getInstance().tmpUserVo.sh_kpi != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.sh_kpi.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "jjsh_gongzuojixiao"
//					&& PermissionModelLocator.getInstance().tmpUserVo.jjsh_gongzuojixiao != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.jjsh_gongzuojixiao.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "erci_gongzuojixiao"
//					&& PermissionModelLocator.getInstance().tmpUserVo.erci_gongzuojixiao != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.erci_gongzuojixiao.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}
//				//==operate
//				else if(PermissionModelLocator.getInstance().rule_name == "pf_jingyingjixiao"
//					&& PermissionModelLocator.getInstance().tmpUserVo.pf_jingyingjixiao != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.pf_jingyingjixiao.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "sh_jingyingjixiao"
//					&& PermissionModelLocator.getInstance().tmpUserVo.sh_jingyingjixiao != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.sh_jingyingjixiao.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "jjsh_jingyingjixiao"
//					&& PermissionModelLocator.getInstance().tmpUserVo.jjsh_jingyingjixiao != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.jjsh_jingyingjixiao.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "erci_jingyingjixiao"
//					&& PermissionModelLocator.getInstance().tmpUserVo.erci_jingyingjixiao != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.erci_jingyingjixiao.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}
//				//==dispense
//				else if(PermissionModelLocator.getInstance().rule_name == "pf_paiban"
//					&& PermissionModelLocator.getInstance().tmpUserVo.pf_paiban != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.pf_paiban.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "sh_paiban"
//					&& PermissionModelLocator.getInstance().tmpUserVo.sh_paiban != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.sh_paiban.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "pf_gongzuojixiaogeren"
//					&& PermissionModelLocator.getInstance().tmpUserVo.pf_gongzuojixiaogeren != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.pf_gongzuojixiaogeren.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "sh_gongzuojixiaogeren"
//					&& PermissionModelLocator.getInstance().tmpUserVo.sh_gongzuojixiaogeren != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.sh_gongzuojixiaogeren.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}
//				//==admin
//				else if(PermissionModelLocator.getInstance().rule_name == "pf_xingzhengkeshi"
//					&& PermissionModelLocator.getInstance().tmpUserVo.pf_xingzhengkeshi != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.pf_xingzhengkeshi.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "sh_xingzhengkeshi"
//					&& PermissionModelLocator.getInstance().tmpUserVo.sh_xingzhengkeshi != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.sh_xingzhengkeshi.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}else if(PermissionModelLocator.getInstance().rule_name == "jjsh_xingzhengkeshi"
//					&& PermissionModelLocator.getInstance().tmpUserVo.jjsh_xingzhengkeshi != null){
//					if(PermissionModelLocator.getInstance().tmpUserVo.jjsh_xingzhengkeshi.indexOf(":"+pvo.id+":") != -1){
//						this.selected = true;
//					}else{
//						this.selected = false;
//					}
//				}
			}else{
				Alert.show(ResourceManager.getInstance().getString("lang", "update_failed"));
				this.selected = false;
			}
			this.data = pvo;
			var rtEvt:ReturnEvent=new ReturnEvent(ReturnEvent.roleUserRefresh);
			rtEvt.dispatch();
			
		}
		
		private function deleteRst(evt:ResultEvent):void{
			if(evt.result as int == 1){
				Alert.show(ResourceManager.getInstance().getString("lang", "update_success"));
			}else{
				Alert.show(ResourceManager.getInstance().getString("lang", "update_failed"));
				this.selected = true;
			}
			this.data = pvo;
			var rtEvt:ReturnEvent=new ReturnEvent(ReturnEvent.roleUserRefresh);
			rtEvt.dispatch();
		}
		
		private function upEvt(event:MouseEvent):void{
			if(this.data is OrganizationVo){
				pvo = this.data as OrganizationVo;
			}else if(this.data is DispenseOrganizationVo){
				pvo = this.data as DispenseOrganizationVo;
			}else if(this.data is OperateDepartmentVo){
				pvo = this.data as OperateDepartmentVo;
			}else if(this.data is AdminDepartmentVo){
				pvo = this.data as AdminDepartmentVo;
			}
			
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "LoginImpl";			
			ro.addEventListener(FaultEvent.FAULT,flt);
			
			if(this.selected){//之前选中的，后面要删除
				ro.addEventListener(ResultEvent.RESULT,deleteRst);
				ro.deleteRule(PermissionModelLocator.getInstance().tmpUserVo,
				PermissionModelLocator.getInstance().rule_name,pvo.id);
			}else{//之前没选中，后面要增加
				ro.addEventListener(ResultEvent.RESULT,insertRst);
				ro.addRule(PermissionModelLocator.getInstance().tmpUserVo,
				PermissionModelLocator.getInstance().rule_name,pvo.id);
			}			
		}
		
		private function flt(evt:FaultEvent):void{
			Alert.show(evt.fault.getStackTrace());
		}
		
		private function showHandler(event:FlexEvent):void{
			if(this.data is OrganizationVo){
				pvo = this.data as OrganizationVo;
			}else if(this.data is DispenseOrganizationVo){
				pvo = this.data as DispenseOrganizationVo;
			}else if(this.data is OperateDepartmentVo){
				pvo = this.data as OperateDepartmentVo;
			}else if(this.data is AdminDepartmentVo){
				pvo = this.data as AdminDepartmentVo;
			}
			if(PermissionModelLocator.getInstance().tmpUserVo == null){
				this.selected = false;
				return;
			}
			
			//==organization
			if(PermissionModelLocator.getInstance().rule_name == "pf_gongzuojixiao"
				&& PermissionModelLocator.getInstance().tmpUserVo.pf_gongzuojixiao != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.pf_gongzuojixiao.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "pf_kpi"
				&& PermissionModelLocator.getInstance().tmpUserVo.pf_kpi != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.pf_kpi.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "sh_gongzuojixiao"
				&& PermissionModelLocator.getInstance().tmpUserVo.sh_gongzuojixiao != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.sh_gongzuojixiao.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "sh_kpi"
				&& PermissionModelLocator.getInstance().tmpUserVo.sh_kpi != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.sh_kpi.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "jjsh_gongzuojixiao"
				&& PermissionModelLocator.getInstance().tmpUserVo.jjsh_gongzuojixiao != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.jjsh_gongzuojixiao.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "erci_gongzuojixiao"
				&& PermissionModelLocator.getInstance().tmpUserVo.erci_gongzuojixiao != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.erci_gongzuojixiao.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}
			//==operate
			else if(PermissionModelLocator.getInstance().rule_name == "pf_jingyingjixiao"
				&& PermissionModelLocator.getInstance().tmpUserVo.pf_jingyingjixiao != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.pf_jingyingjixiao.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "sh_jingyingjixiao"
				&& PermissionModelLocator.getInstance().tmpUserVo.sh_jingyingjixiao != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.sh_jingyingjixiao.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "jjsh_jingyingjixiao"
				&& PermissionModelLocator.getInstance().tmpUserVo.jjsh_jingyingjixiao != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.jjsh_jingyingjixiao.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "erci_jingyingjixiao"
				&& PermissionModelLocator.getInstance().tmpUserVo.erci_jingyingjixiao != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.erci_jingyingjixiao.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}
			//==dispense
			else if(PermissionModelLocator.getInstance().rule_name == "pf_paiban"
				&& PermissionModelLocator.getInstance().tmpUserVo.pf_paiban != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.pf_paiban.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "sh_paiban"
				&& PermissionModelLocator.getInstance().tmpUserVo.sh_paiban != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.sh_paiban.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "pf_gongzuojixiaogeren"
				&& PermissionModelLocator.getInstance().tmpUserVo.pf_gongzuojixiaogeren != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.pf_gongzuojixiaogeren.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "sh_gongzuojixiaogeren"
				&& PermissionModelLocator.getInstance().tmpUserVo.sh_gongzuojixiaogeren != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.sh_gongzuojixiaogeren.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}
			//==admin
			else if(PermissionModelLocator.getInstance().rule_name == "pf_xingzhengkeshi"
				&& PermissionModelLocator.getInstance().tmpUserVo.pf_xingzhengkeshi != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.pf_xingzhengkeshi.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "sh_xingzhengkeshi"
				&& PermissionModelLocator.getInstance().tmpUserVo.sh_xingzhengkeshi != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.sh_xingzhengkeshi.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}else if(PermissionModelLocator.getInstance().rule_name == "jjsh_xingzhengkeshi"
				&& PermissionModelLocator.getInstance().tmpUserVo.jjsh_xingzhengkeshi != null){
				if(PermissionModelLocator.getInstance().tmpUserVo.jjsh_xingzhengkeshi.indexOf(":"+pvo.id+":") != -1){
					this.selected = true;
				}else{
					this.selected = false;
				}
			}
		}
	}
}